package com.hanhanxiaochong.zhongxiangchuanmei.view.my.act;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.hanhanxiaochong.zhongxiangchuanmei.R;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.InfoListBean;
import com.hanhanxiaochong.zhongxiangchuanmei.config.page.PageRecorder;
import com.hanhanxiaochong.zhongxiangchuanmei.presenter.MsgPresenter;
import com.hanhanxiaochong.zhongxiangchuanmei.util.SharedPreferencesUtil;
import com.hanhanxiaochong.zhongxiangchuanmei.view.base.BaseLayout;
import com.hanhanxiaochong.zhongxiangchuanmei.view.iview.IMsgView;
import com.hanhanxiaochong.zhongxiangchuanmei.view.my.adapter.MsgAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Author : 贾桐
 * E-mail : 653773284@qq.com
 * Time   : 2018/4/16.
 * Desc   : 消息-动态公告
 */

public class MsgLayout extends BaseLayout<MsgPresenter> implements IMsgView {

    @BindView(R.id.smart_refresh)
    SmartRefreshLayout smartRefresh;
    @BindView(R.id.recyclerview_msg)
    RecyclerView recyclerviewMsg;

    private String token;
    private PageRecorder pageRecorder;
    private MsgAdapter msgAdapter;

    public MsgLayout(Context context) {
        super(context);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.layout_msg;
    }

    @Override
    protected void initPresenter() {
        mPresenter = new MsgPresenter(this);
    }

    @Override
    protected void init() {
        pageRecorder = new PageRecorder();
        token = SharedPreferencesUtil.getsInstances(mContext).getLoginInfo().getToken();
        recyclerviewMsg.setLayoutManager(new LinearLayoutManager(mContext));
        List<InfoListBean> list = new ArrayList<>();
        msgAdapter = new MsgAdapter(mContext, R.layout.item_msg_announcement, list);
        recyclerviewMsg.setAdapter(msgAdapter);
        msgAdapter.setmOnItemClickListener((adapter, position) -> {
            Intent intent = new Intent(mContext, AnnouncementInfoActivity.class);
            mContext.startActivity(intent);
        });
        smartRefresh.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                mPresenter.getMsgList(token, "1", pageRecorder.getCurrentPage());
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                pageRecorder.moveToFirstPage();
                mPresenter.getMsgList(token, "1", pageRecorder.getCurrentPage());
            }
        });
        mPresenter.getMsgList(token, "1", pageRecorder.getCurrentPage());
    }

    @Override
    public void setList(List<InfoListBean> data) {
        smartRefresh.finishRefresh();
        msgAdapter.setDatas(data);
    }

    @Override
    public void addList(List<InfoListBean> data) {
        smartRefresh.finishLoadmore();
        msgAdapter.addDatas(data);
    }

    @Override
    public void msgDetail(String data) {

    }
}
